package com.summer.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.summer.entity.OrderCollection;
import com.summer.mapper.*;
import com.summer.service.IOrderCollectionService;
import com.summer.util.BuildXLSX;
import com.summer.util.CallBackResult;
import com.summer.util.XlsxParam;
import com.summer.vo.CollectionUserVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * Desc:
 * Created by tl on 2019/1/4
 */
@Controller
@RequestMapping("/v1.0/api/overdueOrder")
public class OrderOverdueController {
    private static Logger log = LoggerFactory.getLogger(OrderOverdueController.class);
    @Resource
    private OrderCollectionDAO orderCollectionDAO;
    @Resource
    private OrderRepaymentMapper orderRepaymentMapper;
    @Resource
    private OrderCollectionReductionDAO orderCollectionReductionDAO;

    @Resource
    private IOrderCollectionService orderCollectionService;
    @Resource
    private OrderCollectionDetailDAO orderCollectionDetailDAO;
    @Resource
    private OrderRepaymentDetailDAO orderRepaymentDetailDAO;

    /**
     * 逾期订单列表
     *
     * @param jsonData
     * @return
     */
    @ResponseBody
    @PostMapping("/queryWithUser")
    public String queryWithUser(@RequestBody String jsonData) {
        Map<String, Object> params = JSONObject.parseObject(jsonData);
        log.info("OrderOverdueController queryWithUser params=" + params.toString());
        PageHelper.startPage(params);
        List<CollectionUserVO> list = orderCollectionDAO.findParams(params);
        return CallBackResult.returnJson(new PageInfo<>(list));
    }
    /**
     * 逾期统计
     *
     * @return
     */
    @ResponseBody
    @PostMapping("/statistic")
    public String statistic() {
        List<Map<String, Object>> list = orderCollectionDAO.countOverdue();
        return CallBackResult.returnJson(new PageInfo<>(list));
    }

    /**
     * 转派
     *
     * @param jsonData
     * @return
     */
    @ResponseBody
    @PostMapping("/dispatch")
    public String dispatch(@RequestBody String jsonData) {
        Map<String, Object> params = JSONObject.parseObject(jsonData);
        log.info("OrderOverdueController dispatch params=" + params.toString());
        OrderCollection orderCollection = checkId(params);
        if (null == orderCollection)
            return CallBackResult.returnJson(CallBackResult.BUSINESS_ERROR, "参数非法");
        //TODO CHECK BACKUSER
        Object platformUserId = params.get("platformUserId");
        if (null == platformUserId)
            return CallBackResult.returnJson(CallBackResult.BUSINESS_ERROR, "参数非法");
        orderCollectionService.dispatch(Integer.parseInt(platformUserId.toString()), orderCollection);
        return CallBackResult.returnJson("操作成功");
    }

    private OrderCollection checkId(Map<String, Object> params) {
        Object idObj = params.get("id");
        if (null == idObj) {
            return null;
        }
        return orderCollectionDAO.selectByPrimaryKey(Integer.parseInt(idObj.toString()));
    }

    /**
     * 导出逾期订单列表
     *
     * @param request
     * @param response
     * @param params
     * @throws Exception
     */
    @RequestMapping("downloadOrderOverdue")
    public void downloadOrderOverdue(HttpServletRequest request, HttpServletResponse response,
                                     @RequestParam Map<String, Object> params) throws Exception {
        log.info("OrderOverdueController downloadOrderOverdue params=" + params.toString());
        List<CollectionUserVO> list = orderCollectionDAO.findParams(params);
        log.info("OrderOverdueController downloadOrderOverdue size=" + list.size());
        String title = "催收订单列表";
        BuildXLSX.setFileDownloadHeader(request, response, title);
        LinkedHashMap<String, String> titleMap = new LinkedHashMap<>();
        titleMap.put("realName", "客户姓名");
        titleMap.put("phone", "手机号码");
        titleMap.put("applyAmount", "借款金额");
        titleMap.put("loanTerm", "借款期限");
        titleMap.put("principalAmount", "应还金额");
        titleMap.put("lateFee", "滞纳金");
        titleMap.put("paidAmount", "已还金额");
        titleMap.put("loanTimeChg", "放款日期");
        titleMap.put("repaymentTimeChg", "预计还款日期");
        titleMap.put("lateDay", "逾期天数");
        titleMap.put("groupLevel", "催收小组");
        titleMap.put("userName", "催收人员");
        titleMap.put("overdueCount", "逾期次数");
        titleMap.put("payStatus", "逾期状态");
        XlsxParam xlsxParam = new XlsxParam(list, title, titleMap);
        OutputStream os = response.getOutputStream();
        BuildXLSX.buildExcel(xlsxParam, os);
    }
}
